package cn.school.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.HashSet;
import java.util.Set;

/**
 * @author oldliu
 * @since 1.0
 */
@WebListener
public class MySessionSet implements HttpSessionAttributeListener {
    //统计多少人在线
    public static final Set<String> onlines=new HashSet<>();
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        if(httpSessionBindingEvent.getName().equals("USER_LOGIN")){
            onlines.add(String.valueOf(httpSessionBindingEvent.getValue()));
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        if(httpSessionBindingEvent.getName().equals("USER_LOGIN")){
            onlines.remove(String.valueOf(httpSessionBindingEvent.getValue()));
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        //>>>httpSessionBindingEvent.getValue()

    }
}
